package com.intelligencesoft.pss.controller.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.intelligencesoft.pss.auth.LoginUtil;
import com.intelligencesoft.pss.entity.base.UserInfo;
import com.intelligencesoft.pss.service.auth.AuthService;
import com.intelligencesoft.pss.utils.auth.AuthStatusCode;
import com.intelligencesoft.pss.utils.auth.ConfigUtil;
import com.intelligencesoft.pss.vo.auth.UserInfoVO;


/**
 * 
 * @author HuangYinglu
 * @date: 2014-8-23-上午1:47:27
 */
@Controller
@RequestMapping(value = "auth")
public class AuthController {

	@Autowired
	private AuthService authService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ConfigUtil config;
	@Autowired
	private LoginUtil loginUtil;

	/**
	 * 登录
	 * 
	 * @param userinfo
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public UserInfoVO login(@ModelAttribute UserInfo userinfo,
			HttpServletResponse response) {
		String msg = config.getAuthConfig("already.logged.in");
		String code = "";
		String info = "";
		String tokenId = "";
		UserInfo u = null;
		try {
			u = authService.login(userinfo);
			info = u.toInfoString();
			loginUtil.setLoginInfo(info, response);
			tokenId = loginUtil.createTokenId(info);
		} catch (IncorrectCredentialsException ice) {
			msg = config.getAuthConfig("login.message.authentication");
			code = AuthStatusCode.PASSWORDERROR.getStatusCode();
		} catch (AuthenticationException ae) {
			msg = config.getAuthConfig("login.message.authentication");
			code = AuthStatusCode.PASSWORDERROR.getStatusCode();
		} catch (Exception e) {
			msg = config.getAuthConfig("login.message.unknownaccount");
			code = AuthStatusCode.NOUSER.getStatusCode();
		}
		return new UserInfoVO(u, msg, code, tokenId);
	}

	/**
	 * 退出
	 * 
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap logout() {
		authService.logout();
		ModelMap map = new ModelMap();
		map.put("result", "logoutSuccess");
		return map;
	}

	/**
	 * 注册
	 * 
	 * @param userinfo
	 * @return
	 */
	@RequestMapping(value = "reg", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap reg(@ModelAttribute UserInfo userinfo) {
		ModelMap map = new ModelMap();
		map.put("result", authService.saveUserinfo(userinfo));
		return map;
	}
	
	/**
	 * 修改密码
	 * 
	 * @param userinfo
	 * @return
	 */
	@RequestMapping(value = "editPassWord", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap editPassWord(@ModelAttribute UserInfo userinfo) {
		ModelMap map = new ModelMap();
		map.put("result", authService.saveUserinfo(userinfo));
		return map;
	}
	/**
	 * 重置密码
	 * @param userInfo
	 * @return
	 */
	@RequestMapping(value = "resetPassWord", method = RequestMethod.GET)
	public ModelMap resetPassword(String id) {
		ModelMap map = new ModelMap();
		UserInfo userInfo=authService.findUserinfoById(id);
		userInfo.setId(id);
		userInfo.setPassword("111111");
		map.put("result", authService.saveUserinfo(userInfo));
		return map;
		
	}

	/**
	 * 跳转到登录页面
	 * @return
	 */
	@RequestMapping(value = "toLogin", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}
}
